import request from '../util/request';

export default {
    namespace: 'generateXml',
    state: {
        tables: [],
        fieldsMap:[[{name:"1",columnName:"1",jdbcType:"1"},{name:"2",columnName:"2",jdbcType:"2"}],[{name:"3",columnName:"3",jdbcType:"3"},{name:"4",columnName:"4",jdbcType:"4"}]]
    },
    reducers : {
        updateTables : function (state, result) { // state：指的是更新之前的状态数据, result: 请求到的数据
            let data=result.data;
            let tableNames=[];
            for(let index in data){
                tableNames[index]=data[index].name;
            }
            state.tables=tableNames;
            return {tables:state.tables,fieldsMap:state.fieldsMap};
        },
        updateFieldsMap: function (state,result) {
             state.fieldsMap=result.data;
             return {tables:state.tables,fieldsMap:state.fieldsMap};
        }
    },
    effects: {
        *getTables(params, sagaEffects) { //定义异步方法
            const {call, put} = sagaEffects; //获取到call、put方法
            const url = "/test/getTablesByName?name="; // 定义请求的url
            let data = yield call(request, url+params.name); //执行请求
            yield put({ // 调用reducers中的方法
                type : "updateTables", //指定方法名
                data : data //传递ajax回来的数据
            });
        },
        *getFieldsMap(params, sagaEffects){
            const {call, put} = sagaEffects; //获取到call、put方法
            const url = "/test/getFieldsByTableName?name="; // 定义请求的url
            let datas=[];
            for(let index in params.selectTables){
                let data = yield call(request, url+params.selectTables[index]); //执行请求
                datas[index]=data;
            }
            yield put({ // 调用reducers中的方法
                type : "updateFieldsMap", //指定方法名
                data : datas //传递ajax回来的数据
            });
        },
        *generateXML(params,sagaEffects){
            const {call, put} = sagaEffects; //获取到call、put方法


        }
    }
}